package in.muton.edushare.poll.pages;

import in.muton.edushare.pages.user.UserPolls;
import in.muton.edushare.poll.panels.IPollPanelContainer;
import in.muton.edushare.poll.reuse.PollReusePage;
import in.muton.edushare.poll.sections.PollEditPanel;
import in.muton.edushare.url.URLLoader;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.vo.Poll;

public class PollEdit extends PollBase implements IPollPanelContainer{

	public PollEdit(PageParameters params) {
		super(params);
		if(getLoggedInUser() != getPollModel().getObject().getCreator()){
			throw new RestartResponseException(URLLoader
					.getPageClass(URLConstants.UNAUTHORIZED_PATH));
		}
		add(new Link<Void>("reusePoll") {
			private static final long serialVersionUID = 1003758838722770766L;

			@Override
			public void onClick() {
				PageParameters params = new PageParameters();
				params.add("0", getPollModel().getObject().getPollId()
						.toString());
				setResponsePage(PollReusePage.class, params);
			}

			@Override
			public boolean isVisible() {
				return getPollModel().getObject().getStatus() == Poll.POLL_STATUS_DRAFT;
			}
		});
		add(new PollEditPanel("pollEditPanel", getPollModel(), this, this));
	}

	@Override
	protected String getPageName() {
		return "PollEdit";
	}

	public void gotoDeletedPage() {
		setResponsePage(UserPolls.class);
	}

	public void gotoNextPage() {
		PageParameters params = new PageParameters();
		params.add("0", getPollModel().getObject().getPollId().toString());
		setResponsePage(PollAudiencesPage.class, params);		
	}

	public void setPoll(IModel<Poll> pollModel) {
		setPollModel(pollModel);
	}
}
